﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using AMStudio.DelegateDispather;
using AMStudio.Common;

namespace AMStudio.UserInterface
{
    static class Program
    {
        public static IUnityContainer ProgramContainer;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (process.MainModule.FileName == current.MainModule.FileName)
                    {
                        MessageBox.Show("The application have already been run!", Application.ProductName,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
            }
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            ProgramContainer = new UnityContainer();
            //获取指定名称的配置节
            //UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            //默认方法，默认获取名称为"unity"配置节下配置信息
            ProgramContainer.LoadConfiguration();
            //container.RegisterType<BaseUI;
            //Form f = container.Resolve<IUserInterface>("BaseUI") as Form;
            //Application.Run(f);
            Application.Run(ProgramContainer.Resolve<AWinFormShell>("BaseUI"));
            //IUnityContainer container = new UnityContainer();
            //container.RegisterType<IEventManager, EventConcerate>("EventManager");
            //container.RegisterType<IEventClient,EventClient>(new TransientLifetimeManager());
            //Application.Run(container.Resolve<Form1>());
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

            Exception ex = e.Exception;
            if (ex != null)
            {
                if (em.HandleException(ex, "Policy"))
                {
                }
                else
                {
                }
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {     
            ExceptionManager em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
            string info=string.Empty;
            Exception ex = e.ExceptionObject as Exception;          
            if (ex != null)
            {
                info = ex.ToString();
                if (em.HandleException(ex, "Policy"))
                {
                }
                else
                {
                }
            }
            else
            {
                info = string.Format("Non-CLS-Compliant exception: Type={0},String={1}", e.ExceptionObject.GetType(), e.ExceptionObject.ToString());
            }


            if (!e.IsTerminating)
            {
            }
            else
            {
                string msg = string.Format("{0} have encountered a problem and " +
                    "need to close. We are sorry for the inconvenience.\n\n" +
                    "Please tell {1} about this proble.\n" +
                    "We have created an error report that you can send to " +
                    "help us improve {0}." +
                    "We will treat this report as confidential and anonymous.\n\n" +
                    "Would you like to send the report?",
                    "(Screw Driver Equipment)", "(IPTE)");

                if (MessageBox.Show(msg, "(EPSLine Station)", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //send the report
                    MessageBox.Show(info, "Error Report");
                }

            }
        }
    }
}
